package com.gml.design.other.strategy;

/**
 * @ClassName : Sorter
 * @Description : 带策略的排序
 * @Author : gml
 * @Date: 2021-04-02 16:33
 */
public class StrategySorter<T> {

    public void sort(T[] arr, Comparator<T> comparable){
        for (int i = 0; i < arr.length-1; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = comparable.compare(arr[j], arr[minPos]) == -1 ? j : minPos;
            }

            swap(arr, i , minPos);
        }
    }

    public void swap(T[] arr, int i, int j){
        T temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

}
